
#region Imported Namespaces
using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Web.UI;
using System.Web.UI.Design;
using System.Web.UI.WebControls;
#endregion

namespace PHSRAG.Insight21.InsightWebControls
{
	/// <summary>
	/// Summary description for PersonnelSubNavigation.
	/// </summary>
	[ToolboxData("<{0}:PersonnelSubNavigation runat=server />")]
	[Designer(typeof(PersonnelSubNavigationDesigner), typeof(IDesigner))]
	public class PersonnelSubNavigation : SubNavigation
	{
		#region Constants
		private const string SelectedPersonnelubNavigationLinkKey = "SelectedPersonnelubNavigationLink_C2AD09F1-8D04-41c4-AE0B-A31BE5259B46";
		#endregion
		#region Constants (Public)
		public const string personnelSummaryCommand = "Personnel Summary";
		public const string salaryGridCommand = "Salary Grid";
		public const string exceptionReportCommand = "Exception Report";
		public const string payrollReportCommand = "Payroll Report";
		public const string expiredFundReportCommand = "Expired Fund Report Link";
		public const string laborDistributionCommand = "Labor Distribution";
		#endregion

		#region Instance Variables
		private LinkButton personnelSummaryLink;
		private LinkButton salaryGridLink;
		private LinkButton exceptionReportLink;
		private LinkButton payrollReportLink;
		private LinkButton expiredFundReportLink;
		private LinkButton laborDistributionLink;
		#endregion

		/// <summary>
		/// Set the selected link so that on load, the appropriate tab may be shown highlighted. This property is invoked
		/// by pages while vectoring to other pages which host this navigation control.
		/// </summary>
		public string SelectedLink
		{
			set { Page.Session[SelectedPersonnelubNavigationLinkKey] = value; }
		}

		#region Implementations for abstract properties and methods from Base Class SubNavigation

		/// <summary>
		/// Create all controls in the PersonnelSubNavigation.
		/// </summary>
		protected override void CreateControls()
		{
			Controls.Clear();

			personnelSummaryLink = new LinkButton();
			personnelSummaryLink.CommandName = personnelSummaryLink.Text = "Personnel Summary";
			personnelSummaryLink.CommandArgument = Utility.Settings.GetKeyValue("SubNavigation.Personnel.PersonnelSummaryURL", "");

			salaryGridLink = new LinkButton();
			salaryGridLink.CommandName = salaryGridLink.Text = "Salary Grid";
			salaryGridLink.CommandArgument = Utility.Settings.GetKeyValue("SubNavigation.Personnel.SalaryGridURL", "");

			exceptionReportLink = new LinkButton();
			exceptionReportLink.CommandName = exceptionReportLink.Text = "Exception Report";
			exceptionReportLink.CommandArgument = Utility.Settings.GetKeyValue("SubNavigation.Personnel.ExceptionReportURL", "");

			payrollReportLink = new LinkButton();
			payrollReportLink.CommandName = payrollReportLink.Text = "Payroll Report";
			payrollReportLink.CommandArgument = Utility.Settings.GetKeyValue("SubNavigation.Personnel.PayrollReportURL", "");

			expiredFundReportLink = new LinkButton();
			expiredFundReportLink.CommandName = expiredFundReportLink.Text = "Expired Fund Report";
			expiredFundReportLink.CommandArgument = Utility.Settings.GetKeyValue("SubNavigation.Personnel.ExpiredFundReportURL", "");

			laborDistributionLink = new LinkButton();
			laborDistributionLink.CommandName = laborDistributionLink.Text = "Labor Distribution";
			laborDistributionLink.CommandArgument = Utility.Settings.GetKeyValue("SubNavigation.Personnel.MonthlyLaborDistributionURL", "");
		}

		/// <summary>
		/// The selected link cell is given a prefix and suffix image as adornments. In addition the background of the
		/// cell is of a different style.
		/// </summary>
		private void CreateSelectedLinkAdornments(TableRow row)
		{
			LinkButton selectedLink = GetSelectedLink();
			if (selectedLink != null)
			{
				foreach (TableCell cell in row.Cells)
				{
					if ((cell.Controls.Count == 1) && (cell.Controls[0] == selectedLink))
					{
						cell.CssClass = "SubModuleNavigationSelectedSummary";

						TableCell adornmentCell = new TableCell();
						adornmentCell.CssClass = "SubModuleNavigationSelectedSummaryAdornment";
						row.Cells.AddAt(row.Cells.GetCellIndex(cell), adornmentCell);

						const string prefixImage = "../Images/Row2_Left.gif";
						System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(Context.Server.MapPath(prefixImage));
						Image image = new Image();
						image.ImageUrl = prefixImage;
						adornmentCell.Width = bitmap.Width;
						adornmentCell.Controls.Add(image);

						adornmentCell = new TableCell();
						adornmentCell.CssClass = "SubModuleNavigationSelectedSummaryAdornment";
						row.Cells.AddAt(row.Cells.GetCellIndex(cell) + 1, adornmentCell);

						const string suffixImage = "../Images/Row2_Right.gif";
						bitmap = new System.Drawing.Bitmap(Context.Server.MapPath(suffixImage));
						image = new System.Web.UI.WebControls.Image();
						image.ImageUrl = suffixImage;
						adornmentCell.Width = bitmap.Width;
						adornmentCell.Controls.Add(image);

						break;
					}
				}
			}
		}

		/// <summary>
		/// Setup the control tree to reflect the actual (and exact) layout of the controls within the PersonnelSubNavigation.
		/// </summary>
		protected override void GenerateControlTree()
		{
			Table table = new Table();
			Controls.Add(table);
			table.CssClass = "SubModuleSummaryNavigation";

			TableRow row = new TableRow();
			table.Rows.Add(row);

			TableCell cell = new TableCell();
			row.Cells.Add(cell);
			cell.CssClass = "SubModuleSummaryNavigationFillerCell";
			cell.Controls.Add(new LiteralControl("&nbsp;"));

			cell = new TableCell();
			row.Cells.Add(cell);

			table = new Table();
			cell.Controls.Add(table);

			row = new TableRow();
			table.Rows.Add(row);

			cell = new TableCell();
			row.Cells.Add(cell);

			cell = new TableCell();
			row.Cells.Add(cell);
			cell.Controls.Add(personnelSummaryLink);

			cell = new TableCell();
			row.Cells.Add(cell);
			cell.Controls.Add(salaryGridLink);

			cell = new TableCell();
			row.Cells.Add(cell);
			cell.Controls.Add(exceptionReportLink);

			cell = new TableCell();
			row.Cells.Add(cell);
			cell.Controls.Add(payrollReportLink);

			cell = new TableCell();
			row.Cells.Add(cell);
			cell.Controls.Add(expiredFundReportLink);

			cell = new TableCell();
			row.Cells.Add(cell);
			cell.Controls.Add(laborDistributionLink);

			CreateSelectedLinkAdornments(row);
		}

		/// <summary>
		/// Retrieve the name of the link that was last clicked on.
		/// </summary>
		/// <returns>Reference to the link button corresponding to the clicked link</returns>
		protected override LinkButton GetSelectedLink()
		{
			// Anytime a link is clicked on, the OnBubbleEvent handler stores the name of the link in the session. Retrieve
			// that name and return the corresponding link button reference.

			object o = Page.Session[SelectedPersonnelubNavigationLinkKey];
			if (o != null)
			{
				string commandName = (string)o;
				if (commandName == personnelSummaryLink.CommandName)
					return personnelSummaryLink;
				if (commandName == salaryGridLink.CommandName)
					return salaryGridLink;
				if (commandName == exceptionReportLink.CommandName)
					return exceptionReportLink;
				if (commandName == payrollReportLink.CommandName)
					return payrollReportLink;
				if (commandName == expiredFundReportLink.CommandName)
					return expiredFundReportLink;
				if (commandName == laborDistributionLink.CommandName)
					return laborDistributionLink;
			}
			return personnelSummaryLink;
		}
		
		/// <summary>
		/// 
		/// </summary>
		protected override string SelectedSubNavigationLinkKey
		{
			get 
			{
				return SelectedPersonnelubNavigationLinkKey;
			}
		}
		
		#region Event Handlers
		/// <summary>
		/// Disable view state.
		/// </summary>
		/// <param name="e">Argument associated with the load event.</param>
		protected override void OnLoad(EventArgs e)
		{
			EnableViewState = false;
			base.OnLoad (e);	
		}
		#endregion

		#endregion
	}
	
	#region Class PersonnelSubNavigationDesigner
	/// <summary>
	/// Designer associated with the control.
	/// </summary>
	public class PersonnelSubNavigationDesigner : ControlDesigner
	{
		#region Overrides
		/// <summary>
		/// Force the control to create its child controls for display at design time.
		/// </summary>
		/// <returns>HTML representation of the control</returns>
		public override string GetDesignTimeHtml()
		{
			((PersonnelSubNavigation)Component).Design();
			return base.GetDesignTimeHtml();
		}
		#endregion
	}
	#endregion
}
